
package fileby.model.impl.sources.impl;

import com.google.inject.Inject;
import fileby.model.impl.sources.FileItem;
import com.google.inject.Singleton;
import fileby.core.IDisplayableFileSource;
import fileby.core.SourceType;
import fileby.util.plugins.InvalidPluginException;
import fileby.util.plugins.Plugin;
import fileby.util.plugins.PluginLoader;
import fileby.plugins.IToolbox;
import fileby.util.plugins.samples.RenamerPlugin;
import java.io.File;
import java.util.List;
import javax.swing.ImageIcon;

/**
 *
 * @author Ludwig Kjellstrom
 */
@Singleton
public class MagicBoxManager extends AbstractFileSource {

    private PluginLoader pluginLoader;
    private Plugin currentPlugin;
    private IToolbox toolbox = null;
    
    @Inject
    public MagicBoxManager(PluginLoader pluginLoader) {
        this.pluginLoader = pluginLoader;
        currentPlugin = new Plugin("Renamer","Renames the files, " +
"from 000 to 999 with correct extension",new RenamerPlugin());
        super.sourceType = SourceType.MAGIC_BOX;
        super.writePermission = true;
        super.changePermission = true;
        super.name = "Magic Box";
        super.icon = new ImageIcon("MagicBoxIcon.png");
    }

    public void loadPlugin(File f) throws InvalidPluginException {
            currentPlugin = pluginLoader.getPlugin(f);
    }

    public Plugin getCurrentPlugin() {
        return currentPlugin;
    }

    /**
     * @pre Must set Toolbox before using this method.
     */
    public void run(){
        if(toolbox == null){
            throw new NullPointerException("Toolbox is null");
        }
        if(currentPlugin.run(toolbox))
            System.out.println(currentPlugin.getName() + " executed successfully.");
        else
            System.out.println(currentPlugin.getName() + " executed failfully.");
    }
    
    public void setToolbox(IToolbox toolbox){
        if(toolbox == null)
            throw new NullPointerException("Toolbox is null");
        this.toolbox = toolbox;
    }

    @Override
    public String getAbsolutePath() {
        return null;
    }

    @Override
    public IDisplayableFileSource getParent() {
        return null;
    }

    @Override
    public boolean addFiles(List<File> fileList, int action) {
        boolean update = false;
        for(File f : fileList) {
            boolean existsAlready = false;
            for (FileItem f2 : files) {
                if (f2.getFile().equals(f)) {
                    existsAlready = true;
                }
            }
            if (existsAlready == false) {
                update = true;
                files.add(new FileItem(f));
            }
        }
         return update;
    }

    @Override
    public void updateContent() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void clear(){
        files.clear();
    }

    public void remove(String path) {
        for (FileItem file : files) {
            if (file.getAbsolutePath().equals(path)) {
                files.remove(file);
                break;
            }
        }
    }

}
